﻿#include "ProgressBarPropertyEditEntry.h"

#include <QProgressBar>
#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(ProgressBar)

#include "IntegerEditEntry.h"
#include "FlagsEditEntry.h"
#include "EnumEditEntry.h"
#include "BooleanEditEntry.h"
#include "UiTextEditEntry.h"

struct ProgressBarPropertyEditEntry::ProgressBarPropertyEditEntryPrivate {
    QPointer<QProgressBar> object;

    QPointer<IntegerEditEntry> entryMinimum;
    QPointer<IntegerEditEntry> entryMaximum;
    QPointer<IntegerEditEntry> entryValue;
    QPointer<FlagsEditEntry> entryAlignment;
    QPointer<BooleanEditEntry> entryTextVisible;
    QPointer<EnumEditEntry> entryOrientation;
    QPointer<BooleanEditEntry> entryInvertedApperance;
    QPointer<EnumEditEntry> entryTextDirection;
    QPointer<UiTextEditEntry> entryFormat;
};

ProgressBarPropertyEditEntry::~ProgressBarPropertyEditEntry() { }

ProgressBarPropertyEditEntry::ProgressBarPropertyEditEntry(QWidget *parent) : PropertyEditEntry {parent} {
    p.reset(new ProgressBarPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
}

void ProgressBarPropertyEditEntry::setObject(void *object) {
    p->object = static_cast<QProgressBar*>(object);
}

void ProgressBarPropertyEditEntry::onTick() {
    if(!p->object || !isVisible()) {
        return ;
    }

    p->entryMinimum->setValue(p->object->minimum());
    p->entryMaximum->setValue(p->object->maximum());
    p->entryValue->setValue(p->object->value());
    p->entryAlignment->setValue(p->object->alignment());
    p->entryTextVisible->setValue(p->object->isTextVisible());
    p->entryOrientation->setValue(p->object->orientation());
    p->entryInvertedApperance->setValue(p->object->invertedAppearance());
    p->entryTextDirection->setValue(p->object->textDirection());
    p->entryFormat->setValue(p->object->format());
}

void ProgressBarPropertyEditEntry::initUi() {
    p->entryMinimum = new IntegerEditEntry;
    appendChildEntry(p->entryMinimum);
    connect(p->entryMinimum, &IntegerEditEntry::valueChanged, [=](int value) {
        p->object->setMinimum(value);
    });

    p->entryMaximum = new IntegerEditEntry;
    appendChildEntry(p->entryMaximum);
    connect(p->entryMaximum, &IntegerEditEntry::valueChanged, [=](int value) {
        p->object->setMaximum(value);
    });

    p->entryValue = new IntegerEditEntry;
    appendChildEntry(p->entryValue);
    connect(p->entryValue, &IntegerEditEntry::valueChanged, [=](int value) {
        p->object->setValue(value);
    });

    p->entryAlignment = new FlagsEditEntry;
    appendChildEntry(p->entryAlignment);
    p->entryAlignment->setMetaEnum(QMetaEnum::fromType<Qt::Alignment>());
    connect(p->entryAlignment, &FlagsEditEntry::valueChanged, [=](int value) {
        p->object->setAlignment(Qt::Alignment(value));
    });

    p->entryTextVisible = new BooleanEditEntry;
    appendChildEntry(p->entryTextVisible);
    connect(p->entryTextVisible, &BooleanEditEntry::valueChanged, [=](bool value) {
        p->object->setTextVisible(value);
    });

    p->entryOrientation = new EnumEditEntry;
    appendChildEntry(p->entryOrientation);
    p->entryOrientation->setMetaEnum(QMetaEnum::fromType<Qt::Orientation>());
    connect(p->entryOrientation, &EnumEditEntry::valueChanged, [=](int value) {
        p->object->setOrientation(Qt::Orientation(value));
    });

    p->entryInvertedApperance = new BooleanEditEntry;
    appendChildEntry(p->entryInvertedApperance);
    connect(p->entryInvertedApperance, &BooleanEditEntry::valueChanged, [=](bool value) {
        p->object->setInvertedAppearance(value);
    });

    p->entryTextDirection = new EnumEditEntry;
    appendChildEntry(p->entryTextDirection);
    p->entryTextDirection->setMetaEnum(QMetaEnum::fromType<QProgressBar::Direction>());
    connect(p->entryTextDirection, &EnumEditEntry::valueChanged, [=](int value) {
        p->object->setTextDirection(QProgressBar::Direction(value));
    });

    p->entryFormat = new UiTextEditEntry;
    appendChildEntry(p->entryFormat);
    connect(p->entryFormat, &UiTextEditEntry::valueChanged, [=](const QString &value) {
        p->object->setFormat(value);
    });
}

void ProgressBarPropertyEditEntry::retranslateUi() {
    setTitle(tr("QProgressBar"));

    p->entryMinimum->setTitle(tr("minimum"));
    p->entryMaximum->setTitle(tr("maximum"));
    p->entryValue->setTitle(tr("value"));
    p->entryAlignment->setTitle(tr("alignment"));
    p->entryTextVisible->setTitle(tr("textVisible"));
    p->entryOrientation->setTitle(tr("orientation"));
    p->entryInvertedApperance->setTitle(tr("invertedApperance"));
    p->entryTextDirection->setTitle(tr("textDirection"));
    p->entryFormat->setTitle(tr("format"));
}
